//
//  BIDWritePostView.m
//  党务通
//
//  Created by mal on 14-1-7.
//  Copyright (c) 2014年 mal. All rights reserved.
//

#import "BIDWritePostView.h"

@interface BIDWritePostView()
{
    BOOL _bConfirm;
}
@end

@implementation BIDWritePostView
@synthesize cancelBtn;
@synthesize confirmBtn;
@synthesize titleLabel;
@synthesize contentTextView;
@synthesize bgView;
@synthesize delegate;

- (id)init
{
    self = [super init];
    if(self)
    {
        [self initView];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)initView
{
    int topSpacing = 10;
    int bottomSpacing = 10;
    int leftSpacing = 10;
    int rightSpacing = 10;
    int itemSpacing = 10;
    [self setBackgroundColor:[UIColor whiteColor]];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGSize viewSize = CGSizeMake(screenSize.width, 140.0f);
    CGRect ownFrame = CGRectMake(0, 0, viewSize.width, viewSize.height);
    self.frame = ownFrame;
    
    CGSize btnSize = CGSizeMake(30, 30);
    cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(leftSpacing, topSpacing, btnSize.width, btnSize.height);
    [cancelBtn setTitle:@"X" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [cancelBtn addTarget:self action:@selector(cancelBtnHandler) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    
    confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(ownFrame.size.width-rightSpacing-btnSize.width, topSpacing, btnSize.width, btnSize.height);
    [confirmBtn setTitle:@"√" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [confirmBtn addTarget:self action:@selector(confirmBtnHandler) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:confirmBtn];
    
    CGSize labelSize = CGSizeMake(60, 30);
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((ownFrame.size.width-labelSize.width)/2, topSpacing, labelSize.width, labelSize.height)];
    titleLabel.text = @"写跟帖";
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [self addSubview:titleLabel];
    
    CGSize textViewSize = CGSizeMake(ownFrame.size.width-leftSpacing-rightSpacing, ownFrame.size.height-topSpacing-bottomSpacing-itemSpacing-titleLabel.frame.size.height);
    contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(leftSpacing, titleLabel.frame.origin.y+titleLabel.frame.size.height+itemSpacing, textViewSize.width, textViewSize.height)];
    contentTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    contentTextView.layer.borderWidth = 1;
    contentTextView.layer.cornerRadius = 3;
    contentTextView.font = [UIFont systemFontOfSize:14.0f];
    [self addSubview:contentTextView];
    
    bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
    [bgView setBackgroundColor:[UIColor colorWithWhite:0.3f alpha:0.7f]];
}

- (void)setTitle:(NSString *)strTitle
{
    titleLabel.text = strTitle;
}

- (void)confirmBtnHandler
{
    _bConfirm = YES;
    [self dismissTheView];
    //[delegate publishPostWithContent:contentTextView.text];
}

- (void)cancelBtnHandler
{
    _bConfirm = NO;
    contentTextView.text = @"";
    [self dismissTheView];
}

- (void)showTheView
{
    _bConfirm = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGRect frame = self.frame;
    frame.origin.y = screenSize.height;
    self.frame = frame;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:bgView];
    [keyWindow addSubview:self];
    [contentTextView becomeFirstResponder];
}

- (void)dismissTheView
{
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    [contentTextView resignFirstResponder];
    [bgView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    CGFloat keyboardHeight = keyboardSize.height;
    __block CGRect frame = self.frame;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    [UIView animateWithDuration:0.3f animations:^{
        frame.origin.y = screenSize.height-keyboardHeight-frame.size.height;
        self.frame = frame;
    } completion:^(BOOL finished){
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    
}

- (void)keyboardDidHide:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    [delegate publishPostWithContent:contentTextView.text];
}

@end
